home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / im2gif / spec.c < prev    next >
Text File  |  1991-11-24  |  4KB  |  195 lines

  1. /* Remove speckles from uosat pics 
  2.  
  3. 21SEP91 NK6K
  4.  
  5. Use after inter.  Usage:
  6.  
  7.     spec infile outfile [cutoff]
  8.  
  9. Get a line.  See if a pixel is more than 20 different from its neighbors.
  10. If so, make it the average of its neighbors.
  11.  
  12. Search for white line by looking in black level area for pixels greater
  13. than cutoff, which defaults to 60.  Adjust the average of any such line
  14. to the average of the last non-white line.  If the line after a white line
  15. is not a white line, and is darker than the previous non-white-line average,
  16. adjust it up to the previous average.
  17.  
  18. The will make the bar be less objectionable.
  19.  
  20.  
  21. */
  22. #include <stdio.h>
  23. #include <dos.h>
  24. #include <fcntl.h>
  25. #include <sys\types.h>
  26. #include <sys\stat.h>
  27.  
  28. #define LINE_SIZE 612
  29. #define NUM_LINE_FIELD 288
  30.  
  31.  
  32. long start_field_2;
  33. int lines=0;
  34. int skip_lines=0;
  35. int correct;
  36. int correct_fudge=0;
  37. long doubles=0;
  38. int bar;
  39. unsigned char buff[LINE_SIZE+1];
  40. int fillval=0;
  41. long fixed=0;
  42. int correct_array[10] = {0,5,5,5,5,5,5,5,5,5};
  43. int cutoff;
  44. int cur_bar=0;
  45. int black=0;
  46.  
  47. main(argc,argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.  
  52.     int fi1,fi2,fo;
  53.     int i;
  54.     long average,averageo;
  55.     int averagei;
  56.  
  57.  
  58.     if (argc<2) {
  59.         printf ("usage: spec input output val\n");
  60.         exit(1);
  61.     }
  62.  
  63.  
  64.     if ((fi1= open(argv[1], O_BINARY)) == -1){
  65.         printf("cannot open: %s\n",argv[1]);
  66.         perror("On input file");
  67.         exit(1);
  68.     }
  69.  
  70.  
  71.  
  72.     if ((fo = open(argv[2], 0)) != -1){
  73.         printf("output file exists.  Aborted\n");
  74.         exit(1);
  75.     }
  76.  
  77.     if ((fo = open(argv[2],O_CREAT | O_TRUNC | O_BINARY | O_RDWR, S_IREAD | S_IWRITE)) == -1) {
  78.         printf("cannot open: %s\n",argv[2]);
  79.         perror("On output file");
  80.         exit(1);
  81.     }
  82.  
  83.     cutoff=60;
  84.     if (argc>3) cutoff = atoi(argv[3]);
  85.     printf("bar detect if > %u\n",cutoff);
  86.  
  87.  
  88.     
  89.     while (1) {        /* Do till there isn't any more */
  90.  
  91.     
  92.  
  93.         if (read(fi1, buff, LINE_SIZE)<LINE_SIZE) {
  94.             break;
  95.         }
  96.  
  97.         averageo=average;
  98.         averagei=average=0l;
  99.         for (i=45;i<LINE_SIZE-1;i++) {
  100.             average+=buff[i];
  101.             averagei++;
  102.             if ( (abs((int)buff[i]-(int)buff[i-1])>20) &&
  103.                  (abs((int)buff[i]-(int)buff[i+1])>20)) {
  104.                 /*printf("line %u:%u (%x %x %x)\n",lines,i,
  105.                     buff[i-1],buff[i],buff[i+1]);*/
  106.               /* check to see if the next pixel isn't a better
  107.                  candidate */
  108.               if ( ( abs((int)buff[i+1]-(int)buff[i-1]) ) >
  109.                  ( abs((int)buff[i]-(int)buff[i-1]))) {
  110.                 doubles++;
  111.               }
  112.               else {
  113.             
  114.                 fixed++;
  115.                     buff[i] = ((unsigned int)buff[i-1]+(unsigned int)buff[i+1])/2;
  116.     
  117.               }
  118.             }
  119.         }
  120.             
  121.         average = average/averagei;
  122. /*        if (buff[34]!=0) {*/
  123. #ifdef OLD
  124.         if (buff[34]>50) {            
  125.             correct=buff[34];
  126.             for (i=0;i<10;i++) correct+=buff[34+i];
  127.             correct/=10;
  128.             correct+=correct_array[correct_fudge];
  129.             correct_fudge++;
  130.             for (i=34;i<LINE_SIZE-1;i++) {
  131.                 if ( ((int) buff[i] - (int) correct)>0)
  132.                     buff[i] = (int) buff[i] - (int) correct    ;
  133.                 else buff[i]=0;
  134.             }
  135.         }
  136. #else
  137.         if ((buff[34]>cutoff) || (buff[36]>cutoff)) {       
  138.                 /* subtract the difference between the average
  139.                 of this line and the average of the previous
  140.                 line. */
  141.         
  142.             cur_bar=1;
  143.               bar++;
  144.             correct = average-averageo;
  145.             average=averageo;
  146.             for (i=34;i<LINE_SIZE-1;i++) {
  147.                 if ( ((int) buff[i] - (int) correct)>0)
  148.                     buff[i] = (int) buff[i] - (int) correct    ;
  149.                 else buff[i]=0;
  150.             }
  151.             for (i=0;i<20;i++) buff[i]=255;
  152.         }
  153.         else {
  154.             if (cur_bar) cur_bar++;
  155.             if (cur_bar && (cur_bar<9)) {
  156.                     /* if we're within 9 lines of the
  157.                     bar, and this line is some limit
  158.                     darker than the previous average,
  159.                     lighten it. */
  160.                 if ((average-averageo)<-2) {
  161.                     black++;
  162.                     correct = averageo-average;
  163.                     average=averageo;
  164.                      for (i=34;i<LINE_SIZE-1;i++) {
  165.                             if ( ((int) buff[i] + (int) correct)<255)
  166.                                     buff[i] = (int) buff[i] + (int) correct    ;
  167.                             else buff[i]=255;
  168.                         }
  169.                     for (i=0;i<20;i++) buff[i]=128;
  170.                 }
  171.             }                    
  172.         }            
  173.  
  174.  
  175. #endif
  176.  
  177.         if (write(fo,buff,LINE_SIZE)==-1) {
  178.             perror("On output file\n");
  179.             exit(1);
  180.         }
  181.  
  182. /*        printf("Line %u fixed %u  \r",lines,fixed);*/
  183.         lines++;
  184.  
  185.     }
  186.     printf("Fixed %lu, doubles %lu\n",fixed,doubles);
  187.     printf("White bar found on %u lines\n",bar);
  188.     printf("Black bar found on %u lines\n",black);
  189.     close (fo);
  190.     close (fi1);
  191.  
  192. }
  193.  
  194.  
  195.